//
//  XTStatusOriginalView.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/15.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class XTStatusOriginalView: UIView {

    //视图底部约束
    var originalViewBottom:Constraint?
    var statusViewModel:XTStatusViewModel?{
        didSet{
        //通过viewModel加载数据
            //获取头像数据
            if let url = statusViewModel?.status?.user?.profile_image_url{
                headImgView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //获取昵称数据
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            //获取时间的数据
            timeLabel.text = statusViewModel?.timeContent
            //获取来源数据
            sourceLabel.text = statusViewModel?.sourceContent
            
            //会员等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            //认证类别
            verifiedTypeImgView.image = statusViewModel?.VerifiedTypeImage
            //获取内容数据
            contentLabel.numberOfLines = 0
            contentLabel.text = statusViewModel?.status?.text
            
            //判断是否有配图
            originalViewBottom?.uninstall()
            if let picUrl = statusViewModel?.status?.pic_urls where picUrl.count>0{
            picView.hidden = false
                picView.picUrls = picUrl
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(picView).offset(margin).constraint
                })
                
            }else{
            picView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(margin).constraint

                })
            }
            
        }
    
    }
    
    
        //  MARK: -懒加载控件-
    //头像
    private lazy var headImgView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证种类
    private lazy var verifiedTypeImgView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel = UILabel(textColor: UIColor.darkGrayColor(), fronSize: 15)
    
    //会员等级
    private lazy var mbrankImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
   
    //时间
    private lazy var timeLabel = UILabel(textColor: UIColor.orangeColor(), fronSize: 12)
    //来源
    private lazy var sourceLabel = UILabel(textColor: UIColor.lightGrayColor(), fronSize: 12)
    //微博内容
    private lazy var contentLabel = UILabel(textColor: UIColor.darkGrayColor(), fronSize: 14)
    //配图
    private lazy var picView: XTStatusPicView = {
    
        let picView = XTStatusPicView()
        picView.backgroundColor = self.backgroundColor
        return picView
    
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setupUI()
   
 }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置控件，添加约束
    func setupUI(){
        
        //添加子控件
        addSubview(headImgView)
        addSubview(screenNameLabel)
        addSubview(verifiedTypeImgView)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picView)
        
        
        //添加约束
        headImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView.snp_trailing).offset(margin)
            make.top.equalTo(self).offset(margin)

        }
        
        verifiedTypeImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImgView.snp_trailing)
            make.centerY.equalTo(headImgView.snp_bottom)
        }
        //会员等级
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(margin)
            make.centerY.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(screenNameLabel.snp_bottom)
            make.leading.equalTo(screenNameLabel)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(margin)
            make.bottom.equalTo(timeLabel)
        }
    
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImgView.snp_bottom).offset(margin)
            make.leading.equalTo(headImgView.snp_leading)
            make.trailing.equalTo(self).offset(-margin)
        }
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.leading.equalTo(contentLabel)
            
        }
        
        
        snp_makeConstraints { (make) -> Void in
            self.originalViewBottom = make.bottom.equalTo(picView).offset(margin).constraint
        }
    }
    

}
